<template>
  <div class="app-container image">
    <div class="image-header">
      <el-button type="primary" size="mini" @click="dialog.createImage = true">新建镜像</el-button>
    </div>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="镜像名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="镜像地址">
        <template slot-scope="scope">
          <span>{{ scope.row.path }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="框架">
        <template slot-scope="scope">
          <span>{{ scope.row.framework }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="place"
        label="语言">
        <template slot-scope="scope">
          <span>{{ scope.row.language }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time ? new Date(Date.parse(new Date(scope.row.create_time))).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="更新时间">
        <template slot-scope="scope">
          <span>{{ scope.row.update_time ? new Date(Date.parse(new Date(scope.row.update_time))).Format("yyyy-MM-dd hh:mm:ss") : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" align="center">
        <template slot-scope="scope">
          <a class="table-action" @click="clickDeleteSource(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <div class="basePagenate">
      <el-pagination
        :page-size="pageSize"
        :current-page="pageNumber"
        :page-count="total"
        layout="prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      :visible.sync="dialog.createImage"
      title="新建镜像"
      width="500px"
      @close="resetForm('createImageForm')">
      <el-form ref="createImageForm" :model="createImageForm" :rules="rulesCreateImage" label-width="100px">
        <el-form-item label="镜像名称：" prop="imageName">
          <el-input v-model="createImageForm.imageName" />
        </el-form-item>
        <el-form-item label="镜像地址：" prop="imageRegistry">
          <el-input v-model="createImageForm.imageRegistry" />
        </el-form-item>
        <el-form-item label="框架：" prop="framework">
          <el-input v-model="createImageForm.framework" />
        </el-form-item>
        <el-form-item label="语言：" prop="language">
          <el-input v-model="createImageForm.language" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.createImage = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('createImageForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { handleImage } from '@/api/source'

export default {
  data() {
    return {
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      list: null,
      listLoading: true,
      dialog: {
        createImage: false
      },
      createImageForm: {
        imageName: '',
        imageRegistry: '',
        framework: '',
        language: ''
      },
      rulesCreateImage: {
        imageName: [
          { required: true, message: '请输入镜像名称', trigger: 'blur' }
        ],
        imageRegistry: [
          { required: true, message: '请输入镜像地址', trigger: 'blur' }
        ],
        framework: [
          { required: true, message: '请输入框架名称', trigger: 'blur' }
        ],
        language: [
          { required: true, message: '请输入语言', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this._handleImage('Get')
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.list = []
      this._handleImage('Get')
    },
    handleCurrentChange(val) {
      this.pageNumber = val
      this.list = []
      this._handleImage('Get')
    },
    // 点击删除资源
    clickDeleteSource(row) {
      this.$confirm(`此操作将删除镜像${row.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          image_registry: row.path
        }
        this._handleImage('Delete', data)
      })
    },
    // 表单提交(createImageForm: 新建镜像)
    submitForm() {
      this.$refs['createImageForm'].validate((valid) => {
        if (valid) {
          const data = {
            image_name: this.createImageForm.imageName,
            image_registry: this.createImageForm.imageRegistry,
            framework: this.createImageForm.framework,
            language: this.createImageForm.language
          }
          this._handleImage('Post', data)
          this.dialog.createImage = false
          this.resetForm('createImageForm')
        } else {
          return false
        }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.dialog.editSource = false
      this.createImageForm = {
        imageName: '',
        imageRegistry: '',
        framework: '',
        language: ''
      }
      this.$refs[formName].resetFields()
    },
    // 镜像操作（查询列表：Get,新增：Post,修改：Put,删除：Delete）
    _handleImage(method, data) {
      this.listLoading = true
      let params = ''
      if (method === 'Get') {
        params = {
          page_index: this.pageNumber,
          per_page_items: this.pageSize
        }
      }
      handleImage(method, data, params).then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          switch (method) {
            case 'Get':
              this.list = []
              if (res.data && res.data.length > 0) {
                const imageArr = []
                res.data.map(item => {
                  if (!this.total) {
                    this.total = item.total_page
                  }
                  if (item.images && item.images.length > 0) {
                    item.images.map(subItem => {
                      let imageItem = ''
                      imageItem = subItem
                      imageItem.framework = item.framework
                      imageItem.language = item.language
                      imageArr.push(imageItem)
                    })
                  }
                })
                this.list = imageArr
              }
              break
            case 'Post':
              this.$message({ message: '新建成功', showClose: true, type: 'success' })
              this.pageNumber = 1
              this._handleImage('Get')
              break
            case 'Put':
              this.$message({ message: '修改成功', showClose: true, type: 'success' })
              this.pageNumber = 1
              this._handleImage('Get')
              break
            case 'Delete':
              this.$message({ message: '删除成功', showClose: true, type: 'success' })
              this.pageNumber = 1
              this._handleImage('Get')
              break
          }
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.image {
  &-header {
    padding: 10px;
    background: #ffffff;
    font-size: 14px;
    color: #303133;
    font-weight: bold;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
    .header-title-left {
      float: right;
      height: 40px;
      line-height: 40px;
    }
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    color: #4285f4;
    &:hover {
      cursor: pointer;
    }
  }
  .status-box {
    border-radius: 5px;
    padding: 0 5px;
    color: #000000;
  }
  .status-success {
    background: #63D4AD;
  }
  .status-run {
    background: #85D8F7;
  }
  .status-wait {
    background: #f39c12;
  }
  .status-stop {
    background: #E0E3E8;
  }
  .status-failure {
    background: #F87D8D
  }
}
</style>
